package com.bhs.sansonglogistics.ui.message;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.view.View;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseActivity;
import com.bhs.sansonglogistics.base.BaseBean;
import com.bhs.sansonglogistics.bean.CircleFriendsBean;
import com.bhs.sansonglogistics.bean.JudgeListDTO;
import com.bhs.sansonglogistics.dialog.CommentInputDialog;
import com.bhs.sansonglogistics.dialog.MyClickListener2;
import com.bhs.sansonglogistics.net.NetApi;
import com.bhs.sansonglogistics.net.NetCallBack;
import com.bhs.sansonglogistics.ui.adapter.CircleFriendsAdapter;
import com.bhs.sansonglogistics.ui.enterprise.CompanyDetailsActivity;
import com.bhs.sansonglogistics.utils.SharedPreferencesUtils;
import com.bhs.sansonglogistics.utils.ToastUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnConfirmListener;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;

/**
 * 圈子复用
 */
public class CircleReuse implements NetCallBack {

    BaseActivity activity;
    public NetApi netApi;
    private int mPosition;
    private String myUnid;
    private CircleFriendsAdapter mAdapter;

    public CircleReuse(BaseActivity activity, String uid) {
        this.activity = activity;
        netApi = activity.netApi;
        this.myUnid = uid;
    }

    public void clickEvent(CircleFriendsAdapter mAdapter) {
        this.mAdapter = mAdapter;
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                mPosition = position;
                CircleFriendsBean data = mAdapter.getData().get(position);
                if (view.getId() == R.id.tv_like || view.getId() == R.id.fl_like) {
                    if (data.getIsAgreed() == 0) {
                        //点赞操作
                        agree(data);
                        networkRequest(netApi.circleAgree(data.getId()), CircleReuse.this, 777);
                    } else {
                        //取消点赞
                        cancelAgree(data);
                        networkRequest(netApi.cancelAgree(data.getId()), CircleReuse.this, 888);
                    }
                    mAdapter.notifyItemChanged(position, 0);

                } else if (view.getId() == R.id.tv_comment || view.getId() == R.id.fl_comment) {
                    priceInputDialog(data);
                } else if (view.getId() == R.id.tv_company_details) {
                    startActivity(new Intent(getActivity(), CompanyDetailsActivity.class)
                            .putExtra("logistics_id", data.getLogistics_id()));
                } else if (view.getId() == R.id.iv_head) {
                    startActivity(new Intent(getActivity(), CirclePersonageActivity.class)
                            .putExtra("uid", data.getUnid())
                            .putExtra("logistics_id", data.getLogistics_id()));
                } else if (view.getId() == R.id.tv_delete) {
                    delete(data);
                }
            }
        });
    }

    private void networkRequest(Call<ResponseBody> dataCall, NetCallBack callBack, int requestCode) {
        activity.networkRequest(dataCall, callBack, requestCode);
    }

    private void startActivity(Intent intent) {
        activity.startActivity(intent);
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        if (requestCode == 777 || requestCode == 888) {
            BaseBean dataBean = new Gson().fromJson(json, BaseBean.class);
            CircleFriendsBean data = mAdapter.getData().get(mPosition);
            //操作失败  数据回调
            if (dataBean.getCode().equals("0")) {
                ToastUtil.show(dataBean.getMsg());
                if (requestCode == 777) {
                    cancelAgree(data);
                } else {
                    agree(data);
                }
                mAdapter.notifyItemChanged(mPosition, 0);
            } else {
                if (requestCode == 777) {
                    ToastUtil.show("点赞成功");
                }
            }
        } else if (requestCode == 111) {
            BaseBean dataBean = new Gson().fromJson(json, BaseBean.class);
            CircleFriendsBean data = mAdapter.getData().get(mPosition);
            if (dataBean.getCode().equals("0")) {
                deleteComment(data);
            } else {
                ToastUtil.show("评论成功");
            }
        } else if (requestCode == 9527) {
            activity.loadingPopupDismiss();
            BaseBean dataBean = new Gson().fromJson(json, BaseBean.class);
            if (dataBean.getCode().equals("1")) {
                ToastUtil.show("删除成功");
                mAdapter.remove(mPosition);
            }
        }
    }

    @Override
    public void onFailure(int requestCode) {

    }

    private Activity getActivity() {
        return activity;
    }

    private Activity getContext() {
        return activity;
    }

    private void cancelAgree(CircleFriendsBean data) {
        data.setIsAgreed(0);
        data.setAgreeCount(data.getAgreeCount() - 1);
        for (int i = 0; i < data.getAgreeList().size(); i++) {
            if (data.getAgreeList().get(i).getUnid().equals(myUnid)) {
                data.getAgreeList().remove(i);
                break;
            }
        }
    }

    private void agree(CircleFriendsBean data) {
        data.setIsAgreed(1);
        data.setAgreeCount(data.getAgreeCount() + 1);
        String headerpic = SharedPreferencesUtils.getString(getContext(), "face");
        String nickname = SharedPreferencesUtils.getString(getContext(), "nickname");
        int logistics_id = SharedPreferencesUtils.getInt(getContext(), "logistics_id", 0);
        data.getAgreeList().add(new CircleFriendsBean.AgreeListDTO(myUnid, headerpic, nickname, logistics_id));
    }


    private CommentInputDialog dialog;

    private void priceInputDialog(CircleFriendsBean data) {
        dialog = new CommentInputDialog(getContext());
        dialog.setMyClickListener(new MyClickListener2() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onClick(String id, String info) {
                Judge(data, info);
                networkRequest(netApi.circleJudge(data.getId(), info), CircleReuse.this, 111);
            }
        });
        new XPopup.Builder(getContext())
                .hasShadowBg(false)
                .autoOpenSoftInput(true)
                .asCustom(dialog)
                .show();
    }

    //评论
    private void Judge(CircleFriendsBean data, String words) {
        String headerpic = SharedPreferencesUtils.getString(getContext(), "company_pic");
        String nickname = SharedPreferencesUtils.getString(getContext(), "nickname");
        data.getJudgeList().add(0, new JudgeListDTO(words, myUnid, headerpic, nickname, System.currentTimeMillis()));
        data.setJudgeCount(data.getJudgeCount() + 1);
        mAdapter.notifyItemChanged(mPosition, 0);
    }

    private void deleteComment(CircleFriendsBean data) {
        data.getJudgeList().remove(data.getJudgeList().size() - 1);
        data.setJudgeCount(data.getJudgeCount() - 1);
        mAdapter.notifyItemChanged(mPosition, 0);
    }

    private void delete(CircleFriendsBean data) {
        new XPopup.Builder(getContext())
                .asConfirm("提示", "确定删除吗",
                        "取消", "确定",
                        new OnConfirmListener() {
                            @SuppressLint("NotifyDataSetChanged")
                            @Override
                            public void onConfirm() {
                                activity.loadingPopupShow();
                                networkRequest(netApi.delCircle(data.getId()), CircleReuse.this, 9527);
                            }
                        }, null, false)
                .show();
    }
}
